package com.eeepay.eeepay_v2.activity;

import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.adapter.AccountAdapter;
import com.eeepay.eeepay_v2.model.AccountInfo;
import com.eeepay.eeepay_v2.util.BaseGrpcTask;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.TimeUtils;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.view.TitleBar;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.eeepay.api.grpc.nano.OutAccountApiProto;
import cn.eeepay.api.grpc.nano.OutAccountApiServiceGrpc;
import io.grpc.ManagedChannel;

/**
 * Created by Ching on 2016/3/17.
 * 账户余额界面
 */
public class AccountActivity extends ABBaseActivity{

    private ListView listView;
    private EditText et_startTime, et_endTime;

    private List<AccountInfo> list;
    private AccountAdapter adapter;

    private TitleBar titleBar;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_account;
    }

    @Override
    protected void initWidget() {

        listView = getViewById(R.id.lv_account);
        titleBar = getViewById(R.id.tb_account);
        titleBar.setRightTextView("确定");
        titleBar.setShowRight(View.VISIBLE);
        titleBar.setRightTextColor(R.color.unify_grounding_white);

        adapter = new AccountAdapter(this);
        listView.setAdapter(adapter);

        et_startTime = getViewById(R.id.et_account_start_time);
        et_startTime.setFocusableInTouchMode(false);
        et_endTime = getViewById(R.id.et_account_end_time);
        et_endTime.setFocusableInTouchMode(false);

        sendHttpRequest(0);
    }

    @Override
    protected void sendHttpRequest(int taskId) {
        showProgressDialog();
        new GrpcTask().execute(Constant.URL.URL, Constant.URL.PORT);
    }

    @Override
    protected void eventOnClick() {
        titleBar.setRightOnClickListener(new TitleBar.RightBtnOnClickListener() {
            @Override
            public void onRightClick(View view) {
                sendHttpRequest(0);
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

        TimeUtils.newDateTimePicker(mContext, et_startTime, TimeUtils.DATE_TIME);
        TimeUtils.newDateTimePicker(mContext, et_endTime, TimeUtils.DATE_TIME);
    }


    private class GrpcTask extends BaseGrpcTask<OutAccountApiProto.OutAccountListRes>{

        @Override
        protected OutAccountApiProto.OutAccountListRes doInBack(ManagedChannel channel) {
            OutAccountApiServiceGrpc.OutAccountApiServiceBlockingStub stub = OutAccountApiServiceGrpc
                    .newBlockingStub(channel)
                    .withDeadlineAfter(Constant.grpc_time_out, TimeUnit.SECONDS);
            OutAccountApiProto.OutAccountReq request = new OutAccountApiProto.OutAccountReq();
            request.accountType = "A";//A代表代理商  M代表商户
            request.userId = "1111111";
            request.subjectNo = "224106";//科目
            request.currencyNo = "1";
            request.recordDate1 = et_startTime.getText().toString().trim();
            request.recordDate2 = et_endTime.getText().toString().trim();
            request.accountOwner = "000001";
            request.selectType = "2";
            OutAccountApiProto.OutAccountListRes response = stub.getOutAccountTransInfo(request);
            return response;
        }

        @Override
        protected void updateUI(OutAccountApiProto.OutAccountListRes result) {
            dismissProgressDialog();
            if(result == null){
                showToast("查询失败");
                return;
            }
            if("false".equals(result.rpcStatus)){
                showToast(result.rpcMsg);
                return;
            }
            OutAccountApiProto.OutAccountRes[] datas = result.outAccountRes;
            if(datas.length > 0){
                list = new ArrayList<>();
                for (int i = 0; i < datas.length; i++) {
                    AccountInfo info = new AccountInfo();
                    info.setTime(datas[i].recordTime);
                    info.setIncome(datas[i].recordAmount);
                    info.setSum(datas[i].balance);
                    info.setUseSum(datas[i].avaliBalance);
                    list.add(info);
                }
                adapter.setList(list);
            }

        }
    }
}
